<?php
use Buan\Config;
use Buan\UrlCommand;
use Buan\View;

$urlRoot = Config::get('ext.Scribble.urlRoot');
View::getGlobalView()->addStylesheets("{$urlRoot}/css/scribble/auth/role.edit.css");
View::getGlobalView()->addJavascripts("{$urlRoot}/js/scribble/auth/role.edit.js");
?>

<div class="head">
	<h1>Roles</h1>
	<h2><?php $this->html->e($role->isInDatabase() ? "Edit {$role->title}" : "Create a role"); ?></h2>
</div>

<form method="post" action="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'auth', 'role', $role->isInDatabase() ? $role->id : 'create')); ?>">
	<div class="std">
		<dl class="required">
			<dt>Title: *</dt>
			<dd><input type="text" name="title" value="<?php $this->html->e($role->title); ?>" /></dd>
		</dl>

		<dl class="required">
			<dt>Description: *</dt>
			<dd><input class="fullwidth" type="text" name="description" value="<?php $this->html->e($role->description); ?>" /></dd>
		</dl>

		<dl>
			<dt>Global Privileges</dt>
			<dt class="aid">These global privileges allow you to define <strong>default role privileges</strong> which are overridden by object-specific privileges</dt>
			<dd id="pallow-all-options">
				<label><input id="pallow_all-1" type="radio" name="pallow_all" value="1" <?php echo $pallowAll==1 ? 'checked="checked"' : ''; ?> />Grant all privileges to users in this role &mdash; <strong>Use with caution!</strong></label><br/>
				<label><input id="pallow_all-0" type="radio" name="pallow_all" value="0" <?php echo $pallowAll!=1 ? 'checked="checked"' : ''; ?> />Let me define the privileges this role has ...</label>
			</dd>
			<dd id="privileges">
				<ul>
				<?php foreach($privileges as $category=>$catPrivs): ?>
					<li>
						<strong><?php $this->html->e($category); ?></strong>
						<ul>
						<?php foreach($catPrivs as $p): ?>
							<li>
								<label><input type="checkbox" name="privilege[]" value="<?php $this->html->e($p->id); ?>" <?php echo isset($rolePrivileges[$p->id]) ? 'checked="checked"' : ''; ?> /><?php $this->html->e($p->name); ?></label>
								<p><?php $this->html->e($p->description); ?></p>
							</li>
						<?php endforeach; ?>
						</ul>
					</li>
				<?php endforeach; ?>
				</ul>
			</dd>
		</dl>
	</div>

	<p>
		<button class="submit" type="submit" value="Save">Save</button>
		<?php if($role->isInDatabase()): ?>
			<button class="delete" type="submit" name="method" value="delete" rel="role-delete" disabled="disabled">Delete</button>
		<?php endif; ?>
	</p>
</form>